package net.caixiaomi.info.app;


import android.app.ActivityManager;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatDelegate;

import com.baidu.mobstat.StatService;
import com.igexin.sdk.PushManager;

import net.caixiaomi.info.Lottery.model.LotteryBettingEntity;
import net.caixiaomi.info.R;
import net.caixiaomi.info.helper.AppHelper;
import net.caixiaomi.info.logger.Timber;
import net.caixiaomi.info.model.UserInfoModel;
import net.caixiaomi.info.service.CXMIntentService;
import net.caixiaomi.info.service.CXMPushService;
import net.caixiaomi.info.util.Constants;
import net.caixiaomi.info.util.DynamicTimeFormat;
import net.caixiaomi.smartrefresh.layout.SmartRefreshLayout;
import net.caixiaomi.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import net.caixiaomi.smartrefresh.layout.api.RefreshHeader;
import net.caixiaomi.smartrefresh.layout.api.RefreshLayout;
import net.caixiaomi.smartrefresh.layout.header.ClassicsHeader;
import net.info.store.SnappyStore;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by mengxianzheng on 2017/11/20.
 */

public class CommonApp extends CrashApp {
    public static SnappyStore STORE;
    private static CommonApp mInstance;

    public static int mScreenWidth;
    public static int mScreenHeight;

    public static List<LotteryBettingEntity> mLotteryBettingList=new ArrayList<>();
    public static int mLotteryIndex=-1;
    public static int mTrebleNum=1;
    public static boolean mAddChip=false;

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);//启用矢量图兼容
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @NonNull
            @Override
            public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.background, R.color.primary_text);//全局设置主题颜色
                return new ClassicsHeader(context).setTimeFormat(new DynamicTimeFormat("更新于 %s")).setDrawableSize(16);
            }
        });
    }

    public static CommonApp getInstance() {
        return mInstance;
    }


    public static UserInfoModel getUserInfo() {
        return STORE.getObject(Constants.KEY_USER_INFO, UserInfoModel.class);
    }

    public static void setUserInfo(UserInfoModel userInfo) {
        STORE.set(Constants.KEY_USER_INFO, userInfo);
    }

    public static void removeUserInfo() {
        STORE.del(Constants.KEY_USER_INFO);
    }

    public static String getToken() {
        UserInfoModel userInfo = getUserInfo();
        if (userInfo != null) {
            return userInfo.getToken();
        } else {
            return "";
        }
    }


    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
//        ARouter.openLog();     // 打印日志
//        ARouter.openDebug();   // 开启调试模式(如果在InstantRun模式下运行，必须开启调试模式！线上版本需要关闭,否则有安全风险)
//        ARouter.init( this ); // 尽可能早，推荐在Application中初始化

        try {
            String processName = getProcessName(this);
            if (processName != null) {
                if (processName.equals(getPackageName())) {
                    STORE = new SnappyStore(this, Constants.PGT_STORE);
                }
            }
            PushManager.getInstance().initialize(this.getApplicationContext(), CXMPushService.class);
            PushManager.getInstance().registerPushIntentService(this.getApplicationContext(), CXMIntentService.class);
            StatService.setAppChannel(this, AppHelper.getChanel(this), true);
            if (Constants.DEBUG) {
                Timber.plant(new Timber.DebugTree());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
//        try {
//            Class atClazz = Class.forName("android.app.ActivityThread");
//            Method method = atClazz.getDeclaredMethod("currentActivityThread", null);
//            Object invoke = method.invoke(null, null);
//            Field field = invoke.getClass().getDeclaredField("mInstrumentation");
//            field.setAccessible(true);
//            field.set(invoke,new CXMInstrumentation());
//        } catch (ClassNotFoundException e) {
//            e.printStackTrace();
//        } catch (NoSuchMethodException e) {
//            e.printStackTrace();
//        } catch (InvocationTargetException e) {
//            e.printStackTrace();
//        } catch (IllegalAccessException e) {
//            e.printStackTrace();
//        } catch (NoSuchFieldException e) {
//            e.printStackTrace();

    }

    private String getProcessName(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo proInfo : runningApps) {
            if (proInfo.pid == android.os.Process.myPid()) {
                if (proInfo.processName != null) {
                    return proInfo.processName;
                }
            }
        }
        return null;
    }


}
